package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.Admin;
import com.gzxw.mogublog.commons.entity.Role;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.RoleMapper;
import com.gzxw.mogublog.xo.service.AdminService;
import com.gzxw.mogublog.xo.service.RoleService;
import com.gzxw.mogublog.xo.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminService adminService;

    @Override
    public Role getById(String roleId) {

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",roleId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<Role> getPageList(RoleVO roleVO) {

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleVO.getKeyword()) && StringUtils.isNotEmpty(roleVO.getKeyword().trim())) {
            queryWrapper.like("role_name", roleVO.getKeyword().trim());
        }
        queryWrapper.eq("status", 1);
        Page<Role> page = new Page<>();
        page.setCurrent(roleVO.getCurrentPage());
        page.setSize(roleVO.getPageSize());
        IPage<Role> pageList = this.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public String addRole(RoleVO roleVO) {
        String roleName = roleVO.getRoleName();
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", roleName);
        Role getRole = this.getOne(queryWrapper);
        if (getRole == null) {
            Role role = new Role();
            role.setRoleName(roleVO.getRoleName());
            role.setCategoryMenuUids(roleVO.getCategoryMenuUids());
            role.setSummary(roleVO.getSummary());
            role.insert();
            return "插入成功";
        }
        return "该实体已经存在";
    }

    @Override
    public String editRole(RoleVO roleVO) {
        String uid = roleVO.getUid();
        Role getRole = this.getById(uid);
        if (getRole == null) {
            return "参数有误";
        }
        getRole.setRoleName(roleVO.getRoleName());
        getRole.setCategoryMenuUids(roleVO.getCategoryMenuUids());
        getRole.setSummary(roleVO.getSummary());
        getRole.setUpdateTime(new Date());
        getRole.updateById();
        return "更新成功";
    }

    @Override
    public String deleteRole(RoleVO roleVO) {

        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.eq("status",1);
        adminQueryWrapper.eq("role_uid",roleVO.getUid());
        int count = adminService.count(adminQueryWrapper);
        if (count>0) {
            return "该角色下还有管理员";
        }
        Role role = this.getById(roleVO.getUid());
        role.setStatus(0);
        role.setUpdateTime(new Date());
        role.updateById();

        return "删除成功";
    }
}
